package com.xtianzhuang.www.controller.study;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.xtianzhuang.www.controller.BaseController;
import com.xtianzhuang.www.entity.request.base.BaseRequest;
import com.xtianzhuang.www.entity.response.UploadStudyNotesResponse;
import com.xtianzhuang.www.entity.response.base.BaseResponse;

@Controller
@RequestMapping("/study")
public class StudyController extends BaseController {
	@RequestMapping(value = "/addStudyNotes")
	public ModelAndView addStudyNotes(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("study/study-notes-import");
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		// 跳回原地址
		return modelAndView;
	}

	@RequestMapping(value = "/addCommodity")
	public ModelAndView addCommodity(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("test/commodity-add");
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		// 跳回原地址
		return modelAndView;
	}

	@RequestMapping(value = "/commodityListMin")
	public ModelAndView commodityListMin(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("test/commodity-list-min");
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		// 跳回原地址
		return modelAndView;
	}

	@RequestMapping(value = "/productList")
	public ModelAndView productList(BaseRequest request, BaseResponse response) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("test/product-list");
		super.executeBase(request, response);
		modelAndView.addObject("res", response);
		// 跳回原地址
		return modelAndView;
	}

	@RequestMapping(value = "/uploadStudyNotes")
	@ResponseBody
	public UploadStudyNotesResponse uploadSQLXml(MultipartFile file, HttpServletRequest request) {
		UploadStudyNotesResponse response = new UploadStudyNotesResponse();
		try {
			studyServiceBiz.checkFile(file);
		// 准备文件夹,获取项目中upload文件夹的路径
		String parentDir = request.getServletContext().getRealPath("upload");
			studyServiceBiz.checkDir(parentDir);
		// 获取原始文件名
		String originalFilename = file.getOriginalFilename();
		logger.info("\t originalFilename=" + originalFilename);
		// 确定最终保存时使用的文件
			String filename = UUID.randomUUID().toString().replace("-", "");
		String suffix = "";
		int beginIndex = originalFilename.lastIndexOf(".");
		if (beginIndex != -1) {
			suffix = originalFilename.substring(beginIndex);
		}
		// 执行保存文件
			File dest = new File(parentDir + "/study/" + subject.getPrincipal().getUserId(), filename + suffix);
			file.transferTo(dest);
			studyServiceBiz.saveStudyNotes(filename, originalFilename, file.getSize(), suffix);
			response.setCode("0000");
			response.setMessage("上传成功");
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			response.setMessage(e.getMessage());
			response.setCode("9999");
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return response;
	}


}
